﻿/*
* Copyright (c) 2008 michiyasu wada
* http://www.seyself.com/
* 
* Distributed under The MIT License.
* [http://www.opensource.org/licenses/mit-license.php]
*/


import com.seyself.utils.StringUtil;
import com.seyself.utils.ArrayUtil;


class com.seyself.utils.ObjectUtil
{
    
    public static function parse( obj:Object ):String
    {
        if( arguments.length <= 1 ){
            var lev = 0;
        } else {
            var lev = arguments[1]+1;
        }
        var str = StringUtil.pow("    ", lev )+ "{\n";
        for(var val in obj){
            if( typeof obj[val] == "object" ){
                str += StringUtil.pow("    ", lev )+ val + " : \n";
                if( obj[val] instanceof Array ){
                    str += ArrayUtil.parse( obj[val] , lev );
                } else {
                    str += parse( obj[val] , lev );
                }
            } else {
                if( typeof obj[val] == "string" ){
                    str += StringUtil.pow("    ", lev )+val +" : \""+ obj[val]+"\" ,\n";
                } else {
                    str += StringUtil.pow("    ", lev )+val +" : "+ obj[val]+" ,\n";
                }
            }
        }
        var tmp = str.split("\n");
        tmp[tmp.length-2] = tmp[tmp.length-2].split(",").join("");
        str = tmp.join("\n");
        if( lev ){
            str += StringUtil.pow("    ", lev )+ "} ,\n";
        } else {
            str += StringUtil.pow("    ", lev )+ "} \n";
        }
        return str;
    }
    
}